import { Collection, Document, Sort } from 'mongodb';
import BaseModel from './MongoBaseModel';

interface IDwUser {
  id?: any;
  name?: string;
  [propName: string]: any;
}

class Model extends BaseModel<IDwUser> {
  constructor() {
    super('dwUser');
  }

  async getListPagination(query, sortFields, pageIndex = 1, pageSize = 10) {
    const db = await this.init();
    const total = await db.countDocuments(query);
    const page = pageIndex;
    const size = pageSize > 100 ? 100 : pageSize;
    let ret = db.find(query);
    if (sortFields) {
      ret = ret.sort(sortFields);
    }
    ret = ret.skip(size * (page - 1)).limit(size);
    const r = await ret.toArray();
    return {
      total,
      data: r,
    };
  }
}

export default new Model();
